<?php
require 'TVB/pms/pdes/common/bootstrap.php';

function checkSignature(){
	if(empty($_REQUEST['video_id'])) {
		http_send_status(405);
		exit();
	}
	return true;
	//http_send_status(405);
}

checkSignature();

/*

$_REQUEST = array
(
    'video_id' => 21,
    'export_id' => 262,
    'destination_id' => 0,
    'status' => 1,
    'signature' => '311bca0667d2e221113a90e356e0d1744a259213',
    'information' => array
        (
            'app_url' => 'rtmpe://streaming.tvb.com/dams',
            'file' => 'mp4:2/export/3/000000000262.mp4',
        )
);

*/
$video_id  = (int)$_REQUEST['video_id'];
$export_id = (int)$_REQUEST['export_id'];

Model::updateBySql(
	'UPDATE video_export SET vi_status=? WHERE video_id=? AND export_id=?',
	array(AC::VIDEO_STATUS_UPLOADED, $video_id, $export_id)
);

$exporting = Model::selectBySql(
	'SELECT id FROM video_export WHERE vi_status<>? AND video_id=?',
	array(AC::VIDEO_STATUS_UPLOADED, $video_id)
);

if(empty($exporting)){
	$video_obj = Model::$dbo['w']->findInstance('video',$video_id);
	$video_obj->source_url = $_REQUEST['information']['app_url'].'*'.$_REQUEST['information']['file'];
	$video_obj->video_params = json_encode($_REQUEST);
	$video_obj->vi_status = AC::VIDEO_STATUS_UPLOADED;
	Model::$dbo['w']->save($video_obj);
}
